<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="活动名称">
                <a-input placeholder="请输入活动名称" v-model="queryParam.name" allowClear></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="16" :sm="24">
              <a-form-item label="有效时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />

                <a-button style="margin-left:10px;" type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button v-for="item of statusList" :key="item.id" :value="item.id"> {{ item.name }} </a-radio-button>
        </a-radio-group>

        <div style="display: flex">
          <a-button
            type="primary"
            style="margin-right: 10px"
            @click="
              $router.push({
                path: '/marketing/seckill/edit'
              })
            "
            >+ 新增</a-button
          >
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('remove', '2')">批量删除</a-menu-item>
              <!-- <a-menu-item @click="handleAll('change', '1')" v-if="queryParam.status != '1'">批量开启</a-menu-item> -->
              <!-- <a-menu-item @click="handleAll('change', '0')" v-if="queryParam.status != '0'">批量关闭</a-menu-item> -->
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>

      <a-table
        :row-key="record => record.id"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        :pagination="{
          total: this.totalCount,
          showSizeChanger: true,
          onChange: this.getList,
          onShowSizeChange: this.paginationSizeChange
        }"
        :data-source="list"
        :loading="loading"
        :columns="columns"
      >
        <span slot="time" slot-scope="text, record">
          {{ record.start_time + '~' + record.end_time }}
        </span>

        <span slot="order_price" slot-scope="text, record">
          {{ '￥' + record.order_price }}
        </span>

        <span slot="status" slot-scope="text, record">
          <a-badge :color="getStatus(record.status)[1]" :text="getStatus(record.status)[0]" />
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="handleItem('edit', record)" size="small">编辑</a-button>
          <a-divider type="vertical" />
          <a-button type="link" @click="handleItem('remove', record)" size="small">删除</a-button>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { List, Save, Status } from '@/api/marketing/seckill'

export default {
  data() {
    return {
      queryParam: {
        name: '',
        start_time: '',
        end_time: '',
        status: '-1'
      },

      queryTime: [],

      labelCol: { span: 4 },
      wrapperCol: { span: 14 },

      form: {
        name: '',
        type: '0',
        times: '',
        days: ''
      },

      rules: {
        name: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择时效设置', trigger: 'change' }],
        times: [{ required: true, message: '请设置天数', trigger: 'change' }],
        days: [{ required: true, message: '请设置天数', trigger: 'blur' }]
      },

      list: [],
      selectedRowKeys: [],

      totalCount: 0,
      pageSize: 10,
      pageNo: 1,
      loading: false,
      visible: false,

      columns: [
        {
          title: 'ID',
          dataIndex: 'id',
          key: 'id',
          width: 50
        },
        {
          title: '活动名称',
          dataIndex: 'name',
          key: 'name',
          ellipsis: true
        },
        {
          title: '有效时间',
          scopedSlots: { customRender: 'time' },
          width: 400
        },
        {
          title: '参与人数',
          dataIndex: 'self_num',
          key: 'self_num',
          sorter: (a, b) => a.self_num - b.self_num
        },
        {
          title: '订单数量',
          dataIndex: 'order_num',
          key: 'order_num',
          sorter: (a, b) => a.order_num - b.order_num
        },
        {
          title: '订单金额',
          dataIndex: 'order_price',
          key: 'order_price',
          scopedSlots: { customRender: 'order_price' },
          sorter: (a, b) => a.order_price - b.order_price
        },
        {
          title: '状态',
          dataIndex: 'status',
          key: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          key: 'action',
          scopedSlots: { customRender: 'action' },
          width: 150
        }
      ],

      statusList: [
        { id: '-1', name: '全部' },
        { id: '1', name: '未开始' },
        { id: '2', name: '进行中' },
        { id: '3', name: '已结束' }
      ]
    }
  },

  computed: {
    getStatus() {
      return status => {
        let text, color
        switch (String(status)) {
          case '1':
            text = '未开始'
            color = '#FAAD14'
            break

          case '2':
            text = '进行中'
            color = '#65C817'
            break

          case '3':
            text = '已结束'
            color = '#C6C6C6'
            break
        }
        return [text, color]
      }
    }
  },

  watch: {
    visible(e) {
      if (!e) {
        this.resetForm()
      }
    }
  },

  methods: {
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = ''
        this.queryParam.end_time = ''
      }
    },

    // 分页器页数修改回调
    paginationSizeChange(current, size) {
      this.pageSize = size
      this.getList(current)
    },

    getList(pageNo = 1) {
      this.loading = true
      let { queryParam, pageSize } = this
      List({
        ...queryParam,
        pageNo,
        pageSize
      })
        .then(res => {
          this.loading = false

          if (!res.code) {
            this.pageNo = pageNo
            this.list = res.data.data
            this.totalCount = res.data.totalCount
            return
          }

          this.$message.error(res.msg)
        })
        .catch(() => {
          this.loading = false
        })
    },

    // 批量操作
    handleAll(type, status) {
      let selectedRowKeys = this.selectedRowKeys

      if (!selectedRowKeys.length) {
        return this.$message.error('请至少选择一条选项！')
      }

      selectedRowKeys = selectedRowKeys.join(',')
      switch (type) {
        case 'remove':
          this.Status(selectedRowKeys, status)
          break
        // case 'change':
        //   this.Status(selectedRowKeys, status)
        //   break
      }
    },

    handleItem(type, scope) {
      switch (type) {
        case 'edit':
          this.$router.push({
            path: `/marketing/seckill/edit?id=${scope.id}`
          })
          break

        case 'remove':
          this.Status(scope.id, '2')
          break

        // case 'change':
        //   this.Status(scope.id, scope.status === '1' ? '0' : '1')
        //   break
      }
    },

    Status(ids, status) {
      // 0.禁用 1.启用 2.删除
      Status({
        ids,
        status
      }).then(res => {
        if (!res.code) {
          this.$message.success('操作成功')
          this.selectedRowKeys = []
          this.getList()
          return
        }

        this.$message.error(res.msg)
      })
    },

    // 选中多行
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let { form } = this

          this.loading = true
          Save(form).then(res => {
            if (!res.code) {
              this.$message.success('操作成功！')
              this.visible = false
              this.loading = false

              this.form = {
                name: '',
                type: '0',
                times: '',
                days: ''
              }

              this.getList()
              return
            }

            this.$message.error(res.msg)
          })
        } else {
          return false
        }
      })
    },

    resetForm() {
      this.$refs.ruleForm.resetFields()
    }
  },

  created() {
    this.getList()
  }
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}
</style>
